package com.cskaoyan.service.wx;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketCommentExample;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.UserInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.wx.comment.WxCommentCountVo;
import com.cskaoyan.bean.vo.wx.comment.WxCommentListVo;
import com.cskaoyan.bean.vo.wx.comment.WxCommentPostBO;
import com.cskaoyan.mapper.MarketCommentMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.mapper.WxCommentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * /**
 *
 * @author 35291
 * @since 2022/11/23 17:18
 */
@Service
public class WxCommentServiceImpl implements WxCommentService{
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    WxCommentMapper wxCommentMapper;

    @Override
    public WxCommentCountVo countCount(Integer valueId, byte type) {
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andTypeEqualTo(type);
        criteria.andValueIdEqualTo(valueId);
        int allCount = (int) marketCommentMapper.countByExample(marketCommentExample);
        MarketCommentExample marketCommentExample1 = new MarketCommentExample();
        MarketCommentExample.Criteria criteria1 = marketCommentExample1.createCriteria();
        criteria1.andTypeEqualTo(type);
        criteria1.andValueIdEqualTo(valueId);
        criteria1.andHasPictureEqualTo(true);
       int hasPicCount = (int) marketCommentMapper.countByExample(marketCommentExample1);
        WxCommentCountVo wxCommentCountVo = new WxCommentCountVo();
        wxCommentCountVo.setAllCount(allCount);
        wxCommentCountVo.setHasPicCount(hasPicCount);
        return wxCommentCountVo;
    }

    @Override
    public void postComment(MarketComment marketComment) {
        // MarketUser user = (MarketUser) session.getAttribute("info");
        // Integer userId = user.getId();
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        Integer userId = user.getId();

        marketComment.setUserId(userId);

        marketComment.setAddTime(new Date());
        marketComment.setUpdateTime(new Date());
        // MarketComment marketComment = new MarketComment();

        // BeanUtils.copyProperties(wxCommentPostBO,marketComment);

        // List<String> picUrls = (List<String>) wxCommentPostBO.getPicUrls();
        // if (picUrls.size()>0){
        //     marketComment.setHasPicture(true);
        // }

        // marketComment.setUserId(userId);
        // Date date = new Date(System.currentTimeMillis());
        // marketComment.setAddTime(date);
        // marketComment.setUpdateTime(date);
        // marketComment.setDeleted(false);
        marketCommentMapper.insertSelective(marketComment);
    }

    @Override
    public CommonData getRelatedGoods(Integer valueId, Integer type, Integer limit, Integer page, Integer showType) {
        PageHelper.startPage(page,limit);
       List<WxCommentListVo> wxCommentListVoList=wxCommentMapper.getCommentAndUserInfo(valueId, type, showType);
        PageInfo pageInfo = new PageInfo(wxCommentListVoList);
        CommonData commonData = new CommonData<WxCommentListVo>();
        commonData.setList(wxCommentListVoList);
        commonData.setPages(pageInfo.getPages());
        commonData.setTotal((int) pageInfo.getTotal());
        commonData.setLimit(pageInfo.getPageSize());
        commonData.setPage(pageInfo.getPageNum());

        return commonData;
    }
}
